package cn.edu.scau.dropship.controller;


import cn.edu.scau.dropship.entity.Menu;
import cn.edu.scau.dropship.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    //查询分页
    @RequestMapping("/selectByPage")
    @ResponseBody
    public List<Menu> selectByPage(HttpServletRequest request,Model model) {
        Integer pageno=Integer.valueOf(request.getParameter("pageNum"));
        Integer pagesize=Integer.valueOf(request.getParameter("pageSize"));

        List<Menu> result=new ArrayList<Menu>();
        List<Menu> list=menuService.getList();

        for(int i=0;i<list.size();i++){
            if(i>=((pageno-1)*pagesize)&&i<(pageno*pagesize)){
                result.add(list.get(i));
            }
        }
        return result;
    }

    @RequestMapping("/selectMenu2")
    @ResponseBody
    public List<Menu> selectMenu2(HttpServletRequest request, HttpServletResponse response,
                                  HttpSession session, Model model) {
        String type = request.getParameter("type");
        int menu_id;
        if (Integer.parseInt(type) == 1)
            menu_id = 1;
        else if (Integer.parseInt(type) == 2)
            menu_id = 6;
        else {
            menu_id = 12;
        }
        List<Menu> menuList = menuService.getList();
        List<Menu> menuList2 = new ArrayList<>();

        for (Menu e : menuList) {
            if (e.getMenuId() == menu_id) {
                menuList2.add(e);
            }
        }
        return menuList2;
    }


    @RequestMapping("/selectMenu")
    @ResponseBody
    public List<Menu> selectMenu(HttpServletRequest request, HttpServletResponse response,
                                 HttpSession session, Model model) {
        List<Menu> menuList = menuService.getList();
        return menuList;
    }


    @RequestMapping("/addMenu")
    @ResponseBody
    public String addMenu(HttpServletRequest request, HttpServletResponse response,
                          HttpSession session, Model model) {

        String name = request.getParameter("name");
        String menu_id = request.getParameter("menu_id");
        String level = request.getParameter("level");

        Menu menu = new Menu();
        menu.setName(name);
        menu.setMenuId(Integer.parseInt(menu_id));
        menu.setLevel(Integer.parseInt(level));
        menu.setStatus(1);
        int result=menuService.insert(menu);
        if(result>0){
            return "添加菜单成功";
        }
        return "添加菜单失败，查看菜单名是否已存在";
    }

    @RequestMapping("/updateMenu")
    @ResponseBody
    public String updateMenu(HttpServletRequest request, HttpServletResponse response,
                             HttpSession session, Model model) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String menu_id = request.getParameter("menu_id");
        String level = request.getParameter("level");
        Menu menu = new Menu();
        menu.setId(Integer.parseInt(id));
        menu.setName(name);
        menu.setMenuId(Integer.parseInt(menu_id));
        menu.setLevel(Integer.parseInt(level));
        menu.setStatus(1);
        menuService.updateByPrimaryKey(menu);
        return "success";
    }


    @RequestMapping("/deteleMenu")
    @ResponseBody
    public String deteleMenu(HttpServletRequest request, HttpServletResponse response,
                             HttpSession session, Model model) {
        String id = request.getParameter("id");
        int result=menuService.deleteByPrimaryKey(Integer.parseInt(id));
        if(result>0){
            return "删除菜单成功!";
        }
        return "删除菜单失败!";
    }

    @RequestMapping("/updateMenuList")
    @ResponseBody
    public String updateMenuList(HttpServletRequest request, HttpServletResponse response,
                                 HttpSession session, Model model) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String menu_id = request.getParameter("menu_id");
        String level = request.getParameter("level");
        String status = request.getParameter("status");

        Menu menu = new Menu();
        menu.setId(Integer.valueOf(id));
        menu.setName(name);
        menu.setMenuId(Integer.valueOf(menu_id));
        menu.setLevel(Integer.valueOf(level));
        menu.setStatus(Integer.valueOf(status));

        menuService.updateByPrimaryKey(menu);

        return "success";
    }
}
